﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using RN.ERP.BLL;
using RN.ERP.DTO;
using System.Web.Services;
using Newtonsoft.Json;

namespace RN.ERP.Web.Material.MaterialProperty
{
    public partial class MPInfo : BasePage
    {
        public override bool IsCheckPermission
        {
            get
            {
                return false;
            }
        }

        MaterialBLL bll = new MaterialBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();
            }
        }

        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            int iMpKind = 3;
            int.TryParse(rbInput.SelectedValue, out iMpKind);
            DTO.MPInfo mpinfo = new DTO.MPInfo()
            {
                bIsQuery = cbIsQuery.Checked,
                cMemo = txtAreMemo.Text,
                cMPCode = hdnPCode.Value.Trim(),
                cMPIInfo = txtMPIInfo.Text,
                cMPIName = txtMPName.Text.Trim(),
                iMPIKind = iMpKind,
                iStatus = 0
            };
            DTO.OperationResult result = bll.AddMPInfo(mpinfo, UserInfo.CorpUserGuid);
            if (result.Result)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "$.ligerDialog.warn('" + result.Message + "','',CloseWin);", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "$.ligerDialog.warn('" + result.Message + "');", true);
            }
        }

        private void InitPage()
        {
            hdnPid.Value = Request.QueryString["pid"];
            int pid = 0;
            int.TryParse(hdnPid.Value, out pid);
            DTO.MateProperty property = bll.GetMaterialProperty(pid);
            txtPName.Text = property.cMPName;
            txtPName.Enabled = false;
            hdnPCode.Value = property.cMPCode;
            string optType = Request.QueryString["type"];
            hdnType.Value = optType;
            switch (optType)
            {
                case "1":
                    InitViewMode(true);
                    break;
                case "2":
                    InitViewMode(false);
                    break;
                default:
                    break;
            }
        }

        private void InitViewMode(bool isLock)
        {
            string mpId = Request.QueryString["mpid"];
            int id = 0;
            if (int.TryParse(mpId, out id))
            {
                hdnId.Value = mpId;
                DTO.MPInfo property = bll.GetMPinfo(id);
                txtAreMemo.Text = property.cMemo;
                txtMPName.Text = property.cMPIName;
                cbIsQuery.Checked = property.bIsQuery;
                rbInput.SelectedValue = property.iMPIKind.ToString();
                txtMPIInfo.Text = property.cMPIInfo;
                if (rbInput.SelectedIndex == 2 || rbInput.SelectedIndex == 3)
                {
                    txtMPIInfo.Enabled = true;
                }
                if (isLock)
                {
                    txtAreMemo.Enabled = false;
                    txtMPName.Enabled = false;
                    cbIsQuery.Enabled = false;
                    rbInput.Enabled = false;
                    txtMPIInfo.Enabled = false;
                    LButSave.Visible = false;
                    LinkButton2.Visible = false;
                }
            }
        }

        protected void rbInput_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (rbInput.SelectedIndex)
            {
                case 2:
                case 3:
                    txtMPIInfo.Enabled = true;
                    break;
                default:
                    txtMPIInfo.Enabled = false;
                    break;
            }
        }

        protected void rbInput_TextChanged(object sender, EventArgs e)
        {
            switch (rbInput.SelectedIndex)
            {
                case 2:
                case 3:
                    txtMPIInfo.Enabled = true;
                    break;
                default:
                    txtMPIInfo.Enabled = false;
                    break;
            }
        }
    }
}